package sjq.com.time;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import sjq.com.time.util.CommandAdapter;
import sjq.com.time.util.DataBase;
import sjq.com.time.util.Time;
import sjq.com.time.util.ViewHolder;

public class MainActivity extends AppCompatActivity {

    private Context context;

    private RecyclerView recyclerView;

    private Button btn;

    private  String hour;
    private String mimute;
    private List<Time> timeList;

    DataBase dataBase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;
        timeList = new ArrayList<>();

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        btn = (Button) findViewById(R.id.btn);

        dataBase = new DataBase(this);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, SecondActivity.class);
                intent.putExtra("type","1");
                startActivity(intent);
            }
        });

    }


    private CommandAdapter adapter;

    @Override
    protected void onResume() {
        super.onResume();

        dataBase.open();
        Cursor c = dataBase.getAllContacts();

        timeList.clear();
        if (c.moveToFirst()) {
            do {
                if (c != null) {

                    DisplayContact(c);
                }
            } while (c.moveToNext());
        }
        dataBase.close();

    }

    public void DisplayContact(final Cursor c)
    {

        Time time = new Time();
        time.setId(c.getString(0));
        time.setHour(c.getString(1));
        time.setMimute(c.getString(2));

        timeList.add(time);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true); //加速
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        adapter = new CommandAdapter<Time>(context, R.layout.item, timeList) {
            @Override
            protected void convert(final ViewHolder holder, final  Time t, int position) {
                holder.setText(R.id.tv, t.getHour() +" : "+t.getMimute());

                holder.setOncClickListener(R.id.main, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, SecondActivity.class);
                        intent.putExtra("hour",t.getHour());
                        intent.putExtra("mimute",t.getMimute());
                        intent.putExtra("type","2");
                        startActivity(intent);
                    }
                });

                holder.setOncClickListener(R.id.sb, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if(t.isOpen()){
                            holder.setChecked(R.id.sb, false);
                            Toast.makeText(context, "关闭"+t.getId(),Toast.LENGTH_LONG).show();
                        }else{
                            holder.setChecked(R.id.sb, true);
                            Toast.makeText(context, "打开"+t.getId(),Toast.LENGTH_LONG).show();
                        }

                    }
                });
            }
        };

        recyclerView.setAdapter(adapter);

    }










}
